/*
 * Created on 2004-02-25
 */
package org.opencube.oms;

import info.fingo.util.Text;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.opencube.oms.util.OMSProperties;
import org.opencube.util.Constants;
import org.opencube.util.Util;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class ResourceHelper
{
	static Logger logger = Logger.getLogger( ResourceHelper.class );
	/** 
	 * The resource file extension 
	 */
	static final String OMS_RESOURCE_FILE_EXTENSION = "res";
	
	/**
	 * The connection file name ( without extension ) 
	 */
	public static final String RESOURCE_CONNECTIONS = "connections"; 

	/* Loads the resource with the given path. The path is prefixed with '/<database_type>/' */
	static String loadResource( String resource, String dbType ) throws OMSResourceException
	{
		try
		{
			String fullPath = getResourcePath( resource, dbType );
			logger.debug( "loading resource: " + resource );
			InputStream stream = OMSData.class.getResourceAsStream( fullPath );
			if (stream==null) { 
				throw new IOException();
			}
			return Util.getInputStreamAsString(stream);
		}
		catch( IOException e )
		{
			throw new OMSResourceException( "Error while '" + getResourcePath(resource,dbType) + "' resource loading", OMSResourceException.CODE_IO, e );
		}
	}
	
	/* Loads application resource from the given path */
	static String loadApplicationResource( String resourcePath, String resourceFile )
	throws FileNotFoundException, IOException
	{
		File f = new File( resourcePath, resourceFile + Constants.STR_DOT + OMS_RESOURCE_FILE_EXTENSION );
		return Util.getFileAsString( f );
	}
	 
	/* Loads properties for the given connection alias */
	public static Properties loadDataSourceProperties( String path, String alias )
	throws OMSResourceException
	{
		File resourceFile = new File( path, RESOURCE_CONNECTIONS + Constants.STR_DOT + OMS_RESOURCE_FILE_EXTENSION );
		try
		{
			OMSProperties props = new OMSProperties();
			props.loadFromFile( resourceFile );
			Properties result = props.getProperties( alias );
			if( result == null )
			{
				throw new OMSResourceException( "No '" + alias + "' connection properties found in the " + resourceFile.getAbsolutePath() + " file", OMSResourceException.CODE_OTHER ); 
			} 
			return result; 
		}
		catch( FileNotFoundException e )
		{
			throw new OMSResourceException( "Error while database properties loading: file = " + resourceFile.getAbsolutePath(), OMSResourceException.CODE_FILE_NOT_FOUND, e );
		}
		catch( IOException e )
		{
			throw new OMSResourceException( "Error while database properties loading: file = " + resourceFile.getAbsolutePath(), OMSResourceException.CODE_IO, e );
		}
	}

	/* Constructs the path for the resource */
	private static String getResourcePath( String resource, String dbType )
	{
		return Constants.STR_SLASH + dbType + Constants.STR_SLASH + Text.substitute( resource, Constants.STR_DOT, Constants.STR_SLASH ) + Constants.STR_DOT + OMS_RESOURCE_FILE_EXTENSION; 
	}
	
	static String getResourceFileName( String resource )
	{
		return resource.substring( resource.lastIndexOf( Constants.STR_DOT ) + 1 ) + Constants.STR_DOT + OMS_RESOURCE_FILE_EXTENSION;
	}
	
	static String getNamespace( String resource, String dbType )
	{
		resource = dbType + Constants.STR_DOT + resource; 
		return resource.substring( 0, resource.lastIndexOf( Constants.STR_DOT ) );
	}
}
